package com.pansoft.openplanet.adapter;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.clj.fastble.data.BleDevice;
import com.efounder.recycleviewhelper.CommonAdapter;
import com.efounder.recycleviewhelper.base.ViewHolder;
import com.efounder.utils.ResStringUtil;
import com.pansoft.openplanet.R;
import com.utilcode.util.LogUtils;

import java.util.List;

/**
 * Created by will on 18-4-24.
 */

public class BlueToothDeviceAdapter extends CommonAdapter<BleDevice> {

    private Context context;
    private ItemClickListener listener;

    public BlueToothDeviceAdapter(Context context, int layoutId, List<BleDevice> datas) {
        super(context, layoutId, datas);
        this.context = context;
    }

    @Override
    protected void convert(ViewHolder holder, final BleDevice bledevice, int position) {
        ImageView imgBlue;
        TextView txtName;
        TextView txtMac;
        LinearLayout llItem;

        llItem = (LinearLayout) holder.getView(R.id.ll_item);

        imgBlue = (ImageView) holder.getView(R.id.img_blue);
        txtName = (TextView) holder.getView(R.id.txt_name);
        txtMac = (TextView) holder.getView(R.id.txt_mac);
        txtName.setText(TextUtils.isEmpty(bledevice.getName()) ? ResStringUtil.getString(R.string.bt_unknow_device) : bledevice.getName());
        txtMac.setText(bledevice.getMac());

        llItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (listener != null) {
                    listener.onItemClick(bledevice);
                }
                LogUtils.i(bledevice.getDevice().getAddress());
            }
        });
    }

    public interface ItemClickListener {
        void onItemClick(BleDevice bledevice);
    }

    public void setOnitemClickListener(ItemClickListener listener) {
        this.listener = listener;
    }
}
